﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using ZX.CRM.Bussiness;
using System.Text;

namespace ZX.WebApp.Crm.Report
{
    public partial class Pop_PubPage : System.Web.UI.Page
    {
        private void beginProgress()
        {
            //根据ProgressBar.htm显示进度条界面
            string templateFileName = Path.Combine(Server.MapPath("."), "ProgressBar.htm");
            StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("GB2312"));
            string html = reader.ReadToEnd();
            reader.Close();
            Response.Write(html);
            Response.Flush();
        }

        private void setProgress(string percent, string branchCompany, string iNum,string goal)
        {
            string jsBlock = "<script>SetPorgressBar('" + percent + "','" + branchCompany + "','" + iNum + "','"+goal+"'); </script>";
            Response.Write(jsBlock);
            Response.Flush();
        }

        private void finishProgress()
        {
            string jsBlock = "<script>SetCompleted();</script>";
            Response.Write(jsBlock);
            Response.Flush();
        }
        BCrmValue bCrmValue = new BCrmValue();
        DataTable dtBcp = new DataTable();
        private string strText;

        public string StrText
        {
            get { return strText; }
            set { strText = value; }
        }
        protected void Page_Init(object sender, System.EventArgs e)
        {
        }
        protected void btnDone_Click(object sender, EventArgs e)
        {
            if (hdnFd.Value == "month")
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "red");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                txtStime.Value = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString();
                txtEtime.Value = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                dtBcp = bCrmValue.ReturnDsSort("",txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
            }else if (hdnFd.Value == "lastmonth")
            {
                lnkLastMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastMonth.Style.Add(HtmlTextWriterStyle.Color, "red");
                lnkLastSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                txtStime.Value = DateTime.Now.AddMonths(-1).AddDays(-DateTime.Now.Day + 1).ToShortDateString();
                txtEtime.Value = DateTime.Now.AddDays(-DateTime.Now.Day).ToShortDateString();
                dtBcp = bCrmValue.ReturnDsSort("",txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
            }
            else if (hdnFd.Value == "year")
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "red");
                lnkLastMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                txtStime.Value = DateTime.Now.Year.ToString() + "-1-1";
                txtEtime.Value = DateTime.Now.Year.ToString() + "-12-31";
                dtBcp = bCrmValue.ReturnDsSort("",txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
            }
            else if (hdnFd.Value == "lastyear")
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastYear.Style.Add(HtmlTextWriterStyle.Color, "red");
                txtStime.Value = DateTime.Now.AddYears(-1).Year.ToString() + "-1-1";
                txtEtime.Value = DateTime.Now.AddYears(-1).Year.ToString() + "-12-31";
                dtBcp = bCrmValue.ReturnDsSort("", txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
            }
            else if (hdnFd.Value == "season")
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                int season = DateTime.Now.Month % 3;
                if (season == 0)
                {
                    txtStime.Value = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-1";
                    txtEtime.Value = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                    dtBcp = bCrmValue.ReturnDsSort("",txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
                }
                else if (season == 1)
                {
                    txtStime.Value = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString();
                    txtEtime.Value = DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString();
                    dtBcp = bCrmValue.ReturnDsSort("",txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
                }
                else if (season == 2)
                {
                    txtStime.Value = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-1";
                    txtEtime.Value = DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString();
                    dtBcp = bCrmValue.ReturnDsSort("",txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
                }
            }
            else if (hdnFd.Value == "lastseason")
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkLastSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                lnkLastYear.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                int season = DateTime.Now.Month % 3;
                if (season == 0)
                {
                    txtStime.Value = DateTime.Now.AddMonths(-3).Year.ToString() + "-" + (DateTime.Now.AddMonths(-3).Month - 2).ToString() + "-1";
                    txtEtime.Value = DateTime.Now.AddMonths(-3).AddMonths(1).AddDays(-DateTime.Now.AddMonths(-3).Day).ToShortDateString();
                    dtBcp = bCrmValue.ReturnDsSort("", txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
                }
                else if (season == 1)
                {
                    txtStime.Value = DateTime.Now.AddMonths(-3).AddDays(-DateTime.Now.AddMonths(-3).Day + 1).ToShortDateString();
                    txtEtime.Value = DateTime.Now.AddMonths(-3).AddMonths(3).AddDays(-DateTime.Now.AddMonths(-3).Day).ToShortDateString();
                    dtBcp = bCrmValue.ReturnDsSort("", txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
                }
                else if (season == 2)
                {
                    txtStime.Value = DateTime.Now.AddMonths(-3).Year.ToString() + "-" + (DateTime.Now.AddMonths(-3).Month - 1).ToString() + "-1";
                    txtEtime.Value = DateTime.Now.AddMonths(-3).AddMonths(2).AddDays(-DateTime.Now.AddMonths(-3).Day).ToShortDateString();
                    dtBcp = bCrmValue.ReturnDsSort("", txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
                }
            }
            else {
                dtBcp = bCrmValue.ReturnDsSort("",txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
            } bind();
        }
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "#458fce");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "red");
                txtStime.Value = DateTime.Now.Year.ToString() + "-1-1";
                txtEtime.Value = DateTime.Now.Year.ToString() + "-12-31";
                dtBcp = bCrmValue.ReturnDsSort("",txtStime.Value, txtEtime.Value, "sp_TotalYj_t");
            }
            bind();
        }

        private void bind()
        {
            beginProgress();
            decimal totalMoney = 0;
            decimal goalMoney = 0;
            //DataTable dtBcp = bCrmValue.ReturnDsSort("","2011-1-02", "2013-06-06", "sp_TotalYj_t");
            if (dtBcp != null && dtBcp.Rows.Count > 0)
            {
                for (int i = 0; i < dtBcp.Rows.Count; i++)
                {
                    if (i == 24)
                    {
                        setProgress(totalMoney.ToString(), dtBcp.Rows[i][0] == null ? "" : dtBcp.Rows[i][0].ToString(), i.ToString(), goalMoney.ToString());
                    }
                    else
                    {
                        setProgress(dtBcp.Rows[i][1] == null ? "" : dtBcp.Rows[i][1].ToString(), dtBcp.Rows[i][0] == null ? "" : dtBcp.Rows[i][0].ToString(), i.ToString(), dtBcp.Rows[i][2] == null ? "" : dtBcp.Rows[i][2].ToString());
                        totalMoney = totalMoney + (decimal)dtBcp.Rows[i][1];
                        goalMoney = goalMoney + (decimal)dtBcp.Rows[i][2];
                    }
                    //此处用线程休眠代替实际的操作，如加载数据等
                    System.Threading.Thread.Sleep(50);
                }
            }
            lblTotalMoney.Text = totalMoney.ToString();
            finishProgress();
        }
        private string creatHTML(int iCount)
        {
            StringBuilder sbd = new StringBuilder("");

            for (int i = 0; i < iCount; i++)
            {
                sbd.Append("<div id='ProgressBarSide" + i.ToString() + "' style='position: absolute; height: 21x; width: 100px;color: Silver; border-width: 1px; border-style: Solid; display: none'><div id='ProgressBar" + i.ToString() + "' style='position: absolute; height: 21px; width: 0%; background-color: #3366FF'></div><div id='ProgressText" + i.ToString() + "' style='position: absolute; height: 21px; width: 100%; text-align: right'></div></div>");
            }

            return sbd.ToString();
        }
    }
}